<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :before-close="onCancel" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色名称">
							<el-input v-model="state.ruleForm.roleName" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色标识">
							<template #label>
								<el-tooltip effect="dark" content="用于 `router/route.ts` meta.roles" placement="top-start">
									<span>角色标识</span>
								</el-tooltip>
							</template>
							<el-input v-model="state.ruleForm.roleKey" placeholder="请输入角色标识" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色描述">
							<el-input v-model="state.ruleForm.describe" type="textarea" placeholder="请输入角色描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单权限">
							<el-tree 
								ref="treeTableRef"
								node-key="name"
								:data="state.menuData"
								:props="state.menuProps"
								show-checkbox
								@check="handleCheckChange"
								class="menu-data-tree"
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { reactive, ref, nextTick } from 'vue';
import { storeToRefs } from 'pinia';
import { useRoutesList } from '/@/stores/routesList';
import { useI18n } from 'vue-i18n';
import { useRoleApi } from '/@/api/role/index';
import { ElMessage } from 'element-plus';

// 引入 api 请求接口
const roleApi = useRoleApi();

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const { t } = useI18n();
const treeTableRef = ref();
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		roleName: '', // 角色名称
		roleKey: '', // 角色标识
		status: true, // 角色状态
		describe: '', // 角色描述
		menuAuth: [] as string[],
		btnAuth: [] as string[],
	},
	menuData: [] as TreeType[],
	menuProps: {
		children: 'children',
		label: function (data: any) {
			return t(data.meta.title)
		},
	},
	auth: [] as string[],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const stores = useRoutesList();
const { routesAllList } = storeToRefs(stores);
const backupData = JSON.parse(JSON.stringify(state.ruleForm))

// 打开弹窗
const openDialog = (type: string, row: RowRoleType) => {
	state.auth = [];
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.auth.push(...state.ruleForm.menuAuth,...state.ruleForm.btnAuth);
		state.dialog.title = '修改角色';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增角色';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	getMenuData();
	nextTick(() => {
		treeTableRef.value.setCheckedKeys(state.auth);
	})
};
// 关闭弹窗
const closeDialog = () => {
	roleDialogFormRef.value.resetFields();
	treeTableRef.value.setCheckedKeys([])
	state.ruleForm = backupData
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') {
		roleApi.addRole(state.ruleForm).then((res) => {
			onSubmitHandle(res)
		})
	} else {
		roleApi.editRole(state.ruleForm).then((res) => {
			onSubmitHandle(res)
		})
	}
};

const onSubmitHandle = (res: any) => {
	// 刷新菜单
	if (res.id == 0) {
		ElMessage.error('操作失败');
		return false;
	}
	closeDialog(); // 关闭弹窗
	emit('refresh');
}

// 获取菜单结构数据
const getMenuData = () => {
	state.menuData = routesAllList.value;
};

const handleCheckChange = () => {
	const nodes = treeTableRef.value.getCheckedNodes();
	state.ruleForm.menuAuth = [];
	state.ruleForm.btnAuth = [];
	nodes.forEach((n: any) => {
		if (n.menuType == 0) {
			state.ruleForm.menuAuth.push(n.name)
		} else {
			state.ruleForm.btnAuth.push(n.name)
		}
	});
}

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
